import Vue from 'vue'
import Router from 'vue-router'
import { getToken } from '@/libs/util'
import store from '@/store'
import routes from './router'
import { LoadingBar } from 'view-design';
Vue.use(Router)

const router = new Router({
    routes
});


const LOGIN_PAGE_NAME = 'login'

router.beforeEach((to, from, next) => {
    LoadingBar.start()
    const token = getToken()
    if (!token && to.name !== LOGIN_PAGE_NAME) {
        console.log("未登陆,就跳转其他页面");
        // 未登录且要跳转的页面不是登录页
        next({
            name: LOGIN_PAGE_NAME // 跳转到登录页
        })
    } else if (!token && to.name === LOGIN_PAGE_NAME) {
        // 未登陆且要跳转的页面是登录页
        console.log("未登陆且要跳转的页面是登录页");
        next() // 跳转
    } else if (token && to.name === LOGIN_PAGE_NAME) {
        console.log("已登录且要跳转的页面是登录页");
        // 已登录且要跳转的页面是登录页
        next({
            name: 'home' // 跳转到home页
        })
    } else {
        // console.log("正常跳转");
        // if (store.state.user.devicesType == null) {
        //     store.dispatch('getAppbaseInfo').then(resp => {
        //         if (resp) {
        //             next();
        //         } else {
        //             next({
        //                 name: LOGIN_PAGE_NAME // 跳转到登录页
        //             })
        //         }
        //     })
        // } else {

        // }
        next();
    }
})

router.afterEach(to => {
    LoadingBar.finish()
    window.scrollTo(0, 0)
})


export default router;